﻿using Domain.Event;
using Domain.IRepositorys;
using Domain.Model;
using DTO;
using Infrastructure.Error;
using Infrastructure.UniOfWorkBase;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using AutoMapper;
using Domain.Services;
using Domain.Types;
using Microsoft.Extensions.Configuration;
using OtherLib.TenXun;
using Utility;
using System.IO;
using Newtonsoft.Json;
using Domain.Config;
using TencentCloud.Ame.V20190916.Models;
using Tlh.Common;
using System.Collections.Generic;

namespace Application.App
{
    public class UserFirendsApplication
    {
        public IUserRepository userRepository { get; set; }
        public IInviteLogRepository inviteLogRepository { get; set; }
        public IInComeWalletLogRepository inComeWalletLogRepository { get; set; }


        public object GetFirendsInfo(int uid)
        {
            var allFirendNum = inviteLogRepository.Entities.Count(i => i.UserID == uid);
            var dayFirendNum = inviteLogRepository.Entities.Count(i => i.UserID == uid && i.CT >= DateTime.Today);

            var types = new[] { FinancialBizType.十二元现金奖励.ToString(), FinancialBizType.升级超级商人佣金.ToString()
                , FinancialBizType.完成任务一级提成.ToString(), FinancialBizType.完成任务二级提成.ToString()
                , FinancialBizType.购买道具一级提成.ToString(), FinancialBizType.购买道具二级提成.ToString() };


            var allFee = inComeWalletLogRepository.Entities.Where(i => i.UserID == uid && types.Contains(i.BizType)).Sum(i => i.ChangeMoney);
            var dayFee = inComeWalletLogRepository.Entities.Where(i => i.UserID == uid && i.CT >= DateTime.Today && types.Contains(i.BizType)).Sum(i => i.ChangeMoney);



            return new { allFirendNum, allFee, dayFirendNum, dayFee };
        }

        public List<FirendsListOutput> GetFirendsList(int uid, int level, int page, int size)
        {
            string[] types = null;
            if (level==1)
            {
                types = new[] { FinancialBizType.十二元现金奖励.ToString(), FinancialBizType.升级超级商人佣金.ToString()
                , FinancialBizType.完成任务一级提成.ToString(), FinancialBizType.购买道具一级提成.ToString() };
            }
            else
            {
                types = new[] { FinancialBizType.完成任务二级提成.ToString(), FinancialBizType.购买道具二级提成.ToString() };
            }

            var list = inComeWalletLogRepository.GetFirendsList(uid, types, page, size);

            //var groups = inComeWalletLogRepository.Entities.Where(i => i.UserID == uid && types.Contains(i.BizType))
            //    .Select(i=>new {i.UserID,i.ChangeMoney,i.CT }).GroupBy(i=>i.UserID).OrderByDescending(i=>i.Max(i=>i.CT));

            //var startPage = page <= 0 ? 0 : page - 1;

            //var list = groups.Skip(startPage * size).Take(size).ToList();

            List<FirendsListOutput> firendsListOutputs = new List<FirendsListOutput>();
            foreach (var item in list)
            {
                if (item.FromUid == 0) continue;

                FirendsListOutput firendsListOutput = new FirendsListOutput();

                var u = userRepository.GetByKey(item.FromUid);
                firendsListOutput.Phone = u.Phone;
                firendsListOutput.CT = u.CT;
                firendsListOutput.UserHeadImg = u.HeadImg;
                firendsListOutput.Fee = (int)item.TotalMoney;

                firendsListOutputs.Add(firendsListOutput);
            }

            return firendsListOutputs;
        }

    }
}
